package com.example.starseas_phone;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

import com.example.starseas_phone.Adapters.Stu_EvaluationlistviewAdapter;
import com.example.starseas_phone.Entity.AllClass;
import com.example.starseas_phone.Entity.Evaluation;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

import org.json.JSONArray;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class Stu_evaluationActivity extends AppCompatActivity {

    private ListView stu_evaluation_list;
    private Stu_EvaluationlistviewAdapter adapter;
    private List<Evaluation> list = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_stu_evaluation);
        initid();
        Intent intent = getIntent();
        String id = intent.getStringExtra("id");
        if (id.equals("1")){
            Stuinitlist();
        }else {
            Deptinitlist();
        }


    }

    private void initid(){
        stu_evaluation_list = (ListView) findViewById(R.id.stu_evaluation_list);
        adapter = new Stu_EvaluationlistviewAdapter(Stu_evaluationActivity.this,R.layout.stu_evaluation_listview,list);
    }

    private void Stuinitlist(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/WorkerEvaluation/GetWorkerEvaluation?token="+Sto.students.getToken())
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");
                    JSONArray jsonArray = new JSONArray(msg);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObjects = jsonArray.getJSONObject(i);
                        int WorkerEvaluationId = jsonObjects.getInt("WorkerEvaluationId");
                        String WorkerId = jsonObjects.getString("WorkerId");
                        String WorkerName = jsonObjects.getString("WorkerName");
                        String StuId= jsonObjects.getString("StuId");
                        int CourseId= jsonObjects.getInt("CourseId");
                        String CourseName=jsonObjects.getString("CourseName");
                        String WorkerEvaluationOption1=jsonObjects.getString("WorkerEvaluationOption1");
                        String WorkerEvaluationOption2=jsonObjects.getString("WorkerEvaluationOption2");
                        String WorkerEvaluationOption3=jsonObjects.getString("WorkerEvaluationOption3");
                        String WorkerEvaluationOption4=jsonObjects.getString("WorkerEvaluationOption4");
                        String WorkerEvaluationOption5=jsonObjects.getString("WorkerEvaluationOption5");
                        String EvaluationOfTime=jsonObjects.getString("EvaluationOfTime");
                        Evaluation evaluation = new  Evaluation(WorkerEvaluationId,WorkerId,WorkerName,StuId,CourseId,CourseName,WorkerEvaluationOption1,WorkerEvaluationOption2,WorkerEvaluationOption3,WorkerEvaluationOption4,WorkerEvaluationOption5,EvaluationOfTime);
                        list.add(evaluation);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.notifyDataSetChanged();
                            stu_evaluation_list.setAdapter(adapter);
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

    private void Deptinitlist(){
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    OkHttpClient client = new OkHttpClient();
                    Request request = new Request.Builder()
                            .url(Sto.NetIP+"/api/WorkerEvaluation/GetWorkerEvaluation?token="+Sto.dept.getToken())
                            .get()
                            .build();
                    Response response = client.newCall(request).execute();
                    String responseString = response.body().string();
                    JSONObject jsonObject = new JSONObject(responseString);
                    String msg = jsonObject.getString("message");
                    JSONArray jsonArray = new JSONArray(msg);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObjects = jsonArray.getJSONObject(i);
                        int WorkerEvaluationId = jsonObjects.getInt("WorkerEvaluationId");
                        String WorkerId = jsonObjects.getString("WorkerId");
                        String WorkerName = jsonObjects.getString("WorkerName");
                        String StuId= jsonObjects.getString("StuId");
                        int CourseId= jsonObjects.getInt("CourseId");
                        String CourseName=jsonObjects.getString("CourseName");
                        String WorkerEvaluationOption1=jsonObjects.getString("WorkerEvaluationOption1");
                        String WorkerEvaluationOption2=jsonObjects.getString("WorkerEvaluationOption2");
                        String WorkerEvaluationOption3=jsonObjects.getString("WorkerEvaluationOption3");
                        String WorkerEvaluationOption4=jsonObjects.getString("WorkerEvaluationOption4");
                        String WorkerEvaluationOption5=jsonObjects.getString("WorkerEvaluationOption5");
                        String EvaluationOfTime=jsonObjects.getString("EvaluationOfTime");
                        Evaluation evaluation = new  Evaluation(WorkerEvaluationId,WorkerId,WorkerName,StuId,CourseId,CourseName,WorkerEvaluationOption1,WorkerEvaluationOption2,WorkerEvaluationOption3,WorkerEvaluationOption4,WorkerEvaluationOption5,EvaluationOfTime);
                        list.add(evaluation);
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            adapter.notifyDataSetChanged();
                            stu_evaluation_list.setAdapter(adapter);
                        }
                    });
                }catch (Exception e){
                    e.printStackTrace();
                }
            }
        }).start();
    }

}